﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using ANGFLib;

namespace ANGF
{
    public partial class FormEditWSFileSelect : Form
    {
        public FormEditWSFileSelect()
        {
            InitializeComponent();
        }

        private void updateTarget()
        {
            comboBoxTargetModule.Items.Clear();
            foreach (var n in State.loadedModules)
            {
                if (n.GetAngfRuntimeXml() == null) continue;    // Systemモジュールに関連付けは無いので無視させる
                comboBoxTargetModule.Items.Add(n);
            }
            if (comboBoxTargetModule.Items.Count > 0) comboBoxTargetModule.SelectedIndex = 0;
        }

        private void updateModuleList()
        {
            if (comboBoxTargetModule.SelectedIndex < 0) return;
            Cursor.Current = Cursors.WaitCursor;
            string xmldoc = MyFileWS.FileWS.GetDirectoryList(((Module)comboBoxTargetModule.Items[comboBoxTargetModule.SelectedIndex]).GetAngfRuntimeXml().id);
            var doc = XDocument.Parse(xmldoc);
            listBoxMyModule.Items.Clear();
            foreach (var n in doc.Descendants("Item"))
            {
                listBoxMyModule.Items.Add(n);
            }
            Cursor.Current = Cursors.Default;
        }

        private void FormEditWSFileSelect_Load(object sender, EventArgs e)
        {
            updateTarget();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (!check()) return;
            Close();
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void comboBoxTargetModule_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index < 0) return;
            e.DrawBackground();
            var module = (Module)this.comboBoxTargetModule.Items[e.Index];
            string s = module.GetXmlModuleData().Name + " (" + module.GetXmlModuleData().Id + ")";
            e.Graphics.DrawString(s, e.Font, new SolidBrush(e.ForeColor), e.Bounds);
            e.DrawFocusRectangle();
        }

        private void comboBoxTargetModule_SelectedIndexChanged(object sender, EventArgs e)
        {
            updateModuleList();
        }

        private void listBoxMyModule_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index < 0) return;
            e.DrawBackground();
            var elem = (XElement)this.listBoxMyModule.Items[e.Index];
            string name = elem.Value;
            string count = elem.Attribute("Count").Value;
            string id = elem.Attribute("Id").Value;
            string s = string.Format("{0} ({1}count) {2}", name, count, id);
            e.Graphics.DrawString(s, e.Font, new SolidBrush(e.ForeColor), e.Bounds);
            e.DrawFocusRectangle();
        }
        private bool check()
        {
            if (this.listBoxMyModule.SelectedIndex < 0)
            {
                MessageBox.Show(this, "対象モジュールが選択されていません");
                return false;
            }
            if (this.comboBoxTargetModule.SelectedIndex < 0)
            {
                MessageBox.Show(this, "拡張対象モジュールが選択されていません");
                return false;
            }
            return true;
        }

        private void buttonDescription_Click(object sender, EventArgs e)
        {
            XNamespace ns = ANGFLib.XmlNamespacesConstants.StdXmlNamespace;
            if (!check()) return;
            Cursor.Current = Cursors.WaitCursor;
            var xmldoc = DownloadResult();
            var desc = xmldoc.Descendants(ns + "description").FirstOrDefault();
            FormReport form;
            if (string.IsNullOrWhiteSpace(desc.Value))
                form = new FormReport("(説明はありません)");
            else
                form = new FormReport(desc.Value);
            Cursor.Current = Cursors.Default;
            form.ShowDialog(this);
        }

        public XDocument DownloadResult()
        {
            string target;
            string module;
            string name;
            GetResultNames(out target, out module, out name);
            var doc = MyFileWS.FileWS.Download(target,module);
            var xmldoc = XDocument.Parse(doc);
            return xmldoc;
        }

        public void GetResultNames(out string target, out string module, out string name)
        {
            Module mod = (Module)this.comboBoxTargetModule.Items[this.comboBoxTargetModule.SelectedIndex];
            XElement elem = (XElement)this.listBoxMyModule.Items[this.listBoxMyModule.SelectedIndex];
            target = mod.GetAngfRuntimeXml().id;
            module = elem.Attribute("Id").Value;
            name = elem.Value;
        }
    }
}
